ImageGear Java PDF
Tutorial: Create Your First Project

In this tutorial, you will learn how to configure a Java project for a console application. You will also learn how to open a PDF and save as a new file.

  1. Make sure that you have installed JDK and ImageGear Java PDF properly. See System Requirements and Installation. You will need to copy a sample.pdf file inside the directory where you will be creating the tutorial sample.
  2. Create a new Java file, and name it (e.g., MyFirstIGJavaPDFProject.java). Insert the following code there:
     
    Copy Code
    import com.accusoft.imagegearpdf.*;
    public class MyFirstIGJavaPDFProject
    {
       private PDF pdf;
       private Document document;
       static
       {
           System.loadLibrary("IgPdf");
       }
       // Application entry point.
       public static void main(String[] args)
       {
           boolean linearized = false;
           String inputPath = "sample.pdf";
           String outputPath = "sample_output.pdf";;
           MyFirstIGJavaPDFProject app = new MyFirstIGJavaPDFProject();
           app.loadAndSave(inputPath, outputPath, linearized);
       }
       // Load and save the PDF file.
       private void loadAndSave(String inputPath, String outputPath, boolean linearized)
       {
           try
           {
               this.initializePdf();
               this.openPdf(inputPath);
               this.savePdf(outputPath, linearized);
           }
           catch (Throwable ex)
           {
               System.err.println("Exception: " + ex.toString());
           }
           finally
           {
               this.terminatePdf();
           }
       }
       // Initialize the PDF session.
       private void initializePdf()
       {
           this.pdf = new PDF();
           this.pdf.initialize();
       }
       // Open input PDF document.
       private void openPdf(String inputPath)
       {
           this.document = new Document();
           this.document.openDocument(inputPath);
       }
       // Save PDF document to the output path.
       private void savePdf(String outputPath, boolean linearized)
       {
           SaveOptions saveOptions = new SaveOptions();
           // Set LINEARIZED attribute as provided by the user.
           saveOptions.setLinearized(linearized);
           this.document.saveDocument(outputPath, saveOptions);
       }
       // Close the PDF document and terminate the PDF session.
       private void terminatePdf()
       {
           if (this.document != null)
           {
               this.document.closeDocument();
               this.document = null;
           }
           if (this.pdf != null)
           {
               this.pdf.terminate();
               this.pdf = null;
           }
       }
    }
    
  3. Now, let’s go over some of the important areas in the sample code with more detail.

    The com.accusoft.imagegearpdf namespace:

    • Allows you to load and save native PDF documents
    • Allows rasterization of PDF pages by converting them to bitmaps and adding raster pages to a PDF document
    • Provides multi-page read and write support for the entire document

    To enable the com.accusoft.imagegearpdf namespace in your project, specify the following directive:

     
    Copy Code
    import com.accusoft.imagegearpdf.*;
    

    To initialize and support processing of PDF files we need:

     
    Copy Code
    // Initialize the PDF session.
    private void initializePdf()
    {
       this.pdf = new PDF();
       this.pdf.initialize();
    }
    
  4. There is one main object that is used in this sample code: The Document that holds the entire loaded document.
     
    Copy Code
    private Document document;
    …
    this.document = new Document();
    this.document.openDocument(inputPath);
    
  5. You can save the loaded document using:
     
    Copy Code
    SaveOptions saveOptions = new SaveOptions();
    // Set LINEARIZED attribute as provided by the user.
    saveOptions.setLinearized(linearized);
    this.document.saveDocument(outputPath, saveOptions);
    

    See About Linearized PDF Files for more information.

  6. Now, you can build and run your sample. Please make sure that you have a PDF file named sample.pdf in the same directory where your sample source resided, or change the inputPath in the sample code so that it points to any existing PDF file.
  7. Now, open the terminal in the directory containing your source file and run the following commands:
    1. Compile
       
      Copy Code
      javac -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar MyFirstIGJavaPDFProject.java
      

      After running this command, you should see a file named MyFirstIGJavaPDFProject.class in your current directory.

    2. Build
       
      Copy Code
      jar cfe MyFirstIGJavaPDFProject.jar MyFirstIGJavaPDFProject MyFirstIGJavaPDFProject.class
      

      After running this command, you should see a file named MyFirstIGJavaPDFProject.jar in your current directory.

    3. Set the environment variable (you only have to do this one time)
       
      Copy Code
      export LD_PRELOAD=$HOME/Accusoft/ImageGearJavaPDF1-64/lib/libIGCORE18.so
      
    4. run
       
      Copy Code
      java -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar:. MyFirstIGJavaPDFProject
      

After running your sample, you should see a new PDF file, named sample_output.pdf, in your current directory.

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback